<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>设计链表--双链表</title>
</head>
<body>

<script type="text/javascript" src="./js/LinkedList2.js"></script>
<!-- <script>
var ListNode = function(val) {
    this.val = val;
    this.prev = null;
    this.next = null;
};

var LinkedList = function() {
    this.head = new ListNode(0);
    this.tail = new ListNode(0);
    this.head.next = this.tail;
    this.tail.prev = this.head;
    this.length = 0;
};



/**
 * 获取链表中第 index 个节点的值。如果索引无效，则返回-1。
 * @param {number} index
 * @return {number}
 */
LinkedList.prototype.get = function(index) {
	if(index < 0 || index > this.length)　{
		return -1;
	}

	let cur = this.head;
	if(2 * index < this.length) {//从前往后找
		for(let i = 0; i < index; i++) {
			cur = cur.next;
		}
	}else {//从后往前找
		cur = this.tail;
		for(let i = 0; i < this.length - index + 1; i++) {
			cur = cur.prev;
		}
	}

	return cur.val;
};

/**
 * addAtHead(val)：在链表的第一个元素之前添加一个值为 val 的节点。插入后，新节点将成为链表的第一个节点。
 * @param {number} val
 * @return {void}
 */
LinkedList.prototype.addAtHead = function(val) {
	let newNode = new ListNode(val);
	let node1 = this.head;//前节点
	let node2 = this.head.next;//后节点
	//共要移动4个指针
	newNode.prev = node1;
	newNode.next = node2;
	node1.next = newNode;
	node2.prev = newNode;

	this.length++;
};

/**
 * addAtTail(val)：将值为 val 的节点追加到链表的最后一个元素。
 * @param {number} val
 * @return {void}
 */
LinkedList.prototype.addAtTail = function(val) {
	let newNode = new ListNode(val);
	let node1 = this.tail.prev;//前节点
	let node2 = this.tail;//后节点
	newNode.prev = node1;
	newNode.next = node2;
	node1.next = newNode;
	node2.prev = newNode;

	this.length++;
};

/**
 * addAtIndex(index,val)：在链表中的第 index 个节点之前添加值为 val  的节点。
 * 如果 index 等于链表的长度，则该节点将附加到链表的末尾。
 * 如果 index 大于链表长度，则不会插入节点。
 * 如果index小于0，则在头部插入节点。
 * @param {number} index 
 * @param {number} val
 * @return {void}
 */
LinkedList.prototype.addAtIndex = function(index, val) {
	if(index > this.length) {
		return;
	}
	if(index < 0) {
		index = 0;
	}
	let newNode = new ListNode(val);
	let node1;//前节点
	let node2;//后节点

	if(2 * index < this.length) {//从前往后找
		node1 = this.head;
		for(let i = 0; i < index; i++) {
			node1 = node1.next;
		}
		node2 = node1.next;
	}else {//从后往前找
		node2 = this.tail;
		for(let i = 0; i < this.length - index; i++) {
			node2 = node2.prev;
		}
		node1 = node2.prev;
	}

	newNode.prev = node1;
	newNode.next = node2;
	node1.next = newNode;
	node2.prev = newNode;
	this.length++;
};

/**
 * 如果索引 index 有效，则删除链表中的第 index 个节点。
 * @param {number} index
 * @return {void}
 */
LinkedList.prototype.deleteAtIndex = function(index) {
	if(index < 0 || index > this.length) {
		return;
	}
	let node1, node2;
	if(2 * index < this.length) {//从前往后找
		node1 = this.head;
		for(let i = 0; i < index; i++) {
			node1 = node1.next;
		}
		node2 = node1.next.next;
	}else {//从后往前找
		node2 = this.tail;
		for(let i = 0; i < this.length - index - 1; i++) {
			node2 = node2.prev;
		}
		node1 = node2.prev.prev;
	}
	node1.next = node2;
	node2.prev = node1;
	this.length--;
};
</script> -->
<script type="text/javascript">
var obj = new LinkedList()
//var param_1 = obj.get(1)
obj.addAtHead(11)
obj.addAtTail(12)
obj.addAtTail(13)
obj.addAtTail(14)
//obj.addAtIndex(3, 22)
obj.deleteAtIndex(3)
console.log(obj);
//console.log(obj.get(4));
//console.log(param_1);
</script>
</body>
</html>